#!/bin/sh

# get the package type.
PKG_TYPE="${1}"

# make sure the type isn't null.
if [ "${PKG_TYPE}" = "" ]; then
	# fail.
	exit 77
fi

# get the package info.
PKG_TITLE=`cat configure.ac | grep 'AC_INIT(' | awk '{print $1}' | sed -e 's/,//g' -e 's/AC_INIT(//g'`
PKG_VERSION=`cat configure.ac | grep 'AC_INIT(' | awk '{print $2}' | sed -e 's/,//g'`
PKG_NAME="${PKG_TITLE}-${PKG_VERSION}-${PKG_TYPE}"
PKG_URL="http://${PKG_TITLE}.googlecode.com/files/${PKG_TITLE}-${PKG_VERSION}.tar.bz2"

# act on various options.
if [ "${PKG_TYPE}" = "slack11.0" ]; then
	
	# make sure makepkg exists.
	DEP_MAKEPKG=`which makepkg 2>&1 | awk '{print $2" "$3}'`
	if [ "${DEP_MAKEPKG}" = "no makepkg" ]; then
		# fail.
		echo "error: your system doesn't have makepkg installed."
		exit 55
	fi
	
	# make a slackware tgz package.
	rm -rf "/tmp/${PKG_NAME}"
	mkdir "/tmp/${PKG_NAME}"
	mkdir "/tmp/${PKG_NAME}/install"
	sudo make DESTDIR="/tmp/${PKG_NAME}" install
	sed -e "s/%VERSION%/${PKG_VERSION}/g" data/pkg/slack-desc.in > "/tmp/${PKG_NAME}/install/slack-desc"
	pushd "/tmp/${PKG_NAME}"
	makepkg --linkadd y --chown y "/tmp/${PKG_TITLE}-${PKG_VERSION}-i486-1.tgz"
	popd
	rm -rf "/tmp/${PKG_NAME}"
	
elif [ "${PKG_TYPE}" = "fc6" ]; then
	
	# update our spec file.
	sed -e "s/%VERSION%/${PKG_VERSION}/g" data/pkg/specfile.fc6.in > "/tmp/${PKG_TITLE}-${PKG_VERSION}-1.${PKG_TYPE}.spec"
	
	# download the source file.
	wget -nv "${PKG_URL}" -O "/usr/src/redhat/SOURCES/${PKG_TITLE}-${PKG_VERSION}.tar.bz2"
	
	# run the commands required to make the rpm.
	rpmbuild -ba "/tmp/${PKG_TITLE}-${PKG_VERSION}-1.${PKG_TYPE}.spec"
	
elif [ "${PKG_TYPE}" = "edgy" ]; then
	
	# make sure we have a source tarball.
	if [ ! -f "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2" ]; then
		# get the source package.
		wget -nv "${PKG_URL}" -O "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2"
	fi
	
	# run the debian directory maker.
	env DEBFULLNAME="Bradley Worley" DEBEMAIL="geekysuavo@gmail.com" \
	dh_make -c GPL -s -p "${PKG_TITLE}" -f "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2"
	
	# cheat a bit and replace the debian/control file with a premade one.
	sed -e "s/%VERSION%/${PKG_VERSION}/g" data/pkg/control.edgy.in > debian/control
	
	# run the package creation routine.
	dpkg-buildpackage

	# rename the package to meet our liking.
	rename "s/${PKG_VERSION}-1/${PKG_VERSION}-1edgy1/g" "../*.deb"
	
elif [ "${PKG_TYPE}" = "feisty" ]; then
	
	# make sure we have a source tarball.
	if [ ! -f "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2" ]; then
		# get the source package.
		wget -nv "${PKG_URL}" -O "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2"
	fi
	
	# run the debian directory maker.
	env DEBFULLNAME="Bradley Worley" DEBEMAIL="geekysuavo@gmail.com" \
	dh_make -c GPL -s -p "${PKG_TITLE}" -f "../${PKG_TITLE}-${PKG_VERSION}.tar.bz2"
	
	# cheat a bit and replace the debian/control file with a premade one.
	sed -e "s/%VERSION%/${PKG_VERSION}/g" data/pkg/control.feisty.in > debian/control
	
	# run the package creation routine.
	dpkg-buildpackage

	# rename the package to meet our liking.
	rename "s/${PKG_VERSION}-1/${PKG_VERSION}-1feisty1/g" "../*.deb"
	
elif [ "${PKG_TYPE}" = "cleanup" ]; then
	
	# cleanup.
	rm -f slack-desc "${PKG_TITLE}-${PKG_VERSION}-1.fc6.spec"

else
	
	# fail.
	exit 77
	
fi
